package cn.maihe.elg.operation.service.promotion;

import cn.maihe.elg.operation.model.vo.promotion.ChannelReportQueryVO;
import cn.maihe.elg.operation.model.vo.promotion.ChannelReportVO;
import cn.maihe.elg.operation.repository.entity.ExtendChannelDO;
import cn.maihe.elg.operation.repository.entity.ExtendChannelVo;
import cn.maihe.elg.operation.repository.mapper.AcceptOrderInfoMapper;
import cn.maihe.elg.operation.repository.mapper.ExtendChannelMapper;
import cn.maihe.elg.operation.service.BaseDaoService;
import cn.maihe.elg.operation.utils.DateUtil;
import cn.maihe.framework.starter.base.sql.PageBo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description 推广渠道服务
 * @Author WJH
 * @Date 2021/03/25
 */
@Service
public class ExtendChannelService extends BaseDaoService<ExtendChannelDO> {
    @Resource
    private ExtendChannelMapper exchangeChannelMapper;
    @Resource
    private AcceptOrderInfoMapper acceptOrderInfoMapper;

    public PageBo<ExtendChannelVo> selectPageBySql(int pageNo, int pageSize, ExtendChannelVo vo) {
        Map<String, Object> paramsMap = toMapAndAddParams(vo);
        return this.selectPageBySql("selectExtendChannelList", "selectExtendChannelCount", paramsMap, pageNo, pageSize);
    }

    public ExtendChannelVo getExtendChannelDetailById(Long id) {
        return exchangeChannelMapper.selectById(id).buildVo();
    }


    public PageBo<ChannelReportVO> selectReportPageBySql(int pageNo, int pageSize, String gmtTimeRange, ExtendChannelVo vo) {
        Map<String, Object> paramsMap = toMapAndAddParams(vo);
        if (StringUtils.isNotBlank(gmtTimeRange)) {
            String[] rangeAy = gmtTimeRange.split(",");
            if (rangeAy.length > 1) {
                paramsMap.put("startGmtTime", DateUtil.getDateFromString(rangeAy[0] + " 00:00:00",DateUtil.FORMAT_DATETIME));
                paramsMap.put("endGmtTime", DateUtil.getDateFromString(rangeAy[1] + " 23:59:59",DateUtil.FORMAT_DATETIME));
            }
        }
        PageBo<ExtendChannelVo>  extendChannelVoPageBo =  this.selectPageBySql("selectExtendChannelList", "selectExtendChannelCount", paramsMap, pageNo, pageSize);
        List<String> codes = extendChannelVoPageBo.getList().stream().map(ExtendChannelVo::getInviteCode).collect(Collectors.toList());
        if(CollectionUtils.isEmpty(codes)){
            PageBo<ChannelReportVO> channelReportVOPageBo =
                    new PageBo<ChannelReportVO>(0,
                            0,new ArrayList<>());

            return channelReportVOPageBo;
        }
        paramsMap.put("inviteCodes",codes);
        List<Map> maps = acceptOrderInfoMapper.selectChannelReport(paramsMap);
        int size = maps.size();
        List<ChannelReportVO> channelReportVOS = extendChannelVoPageBo.getList().stream().map(
                item -> {
                    ChannelReportVO channelReportVO = new ChannelReportVO();
                    channelReportVO.setChannelName(item.getChannelName());
                    channelReportVO.setLinkmanMobile(item.getLinkmanMobile());
                    channelReportVO.setLinkmanName(item.getLinkmanName());
                    channelReportVO.setBidCount(0L);
                    channelReportVO.setGuaranteeCount(0L);
                    for(int i=0;i<size;i++){
                        Map map = maps.get(i);
                        if(item.getInviteCode().equals(map.get("invite_code"))){
                            channelReportVO.setBidCount((Long) map.get("bidcount"));
                            channelReportVO.setGuaranteeCount((Long) map.get("guaranteecount"));
                        }
                    }
                    return channelReportVO;
                }
        ).collect(Collectors.toList());
        PageBo<ChannelReportVO> channelReportVOPageBo =
                new PageBo<ChannelReportVO>(extendChannelVoPageBo.getTotal(),
                        extendChannelVoPageBo.getPages(),channelReportVOS);

        return channelReportVOPageBo;
    }

    public List<ChannelReportVO> listChannelReportDownload(ChannelReportQueryVO vo) {
        Map<String, Object> paramsMap = toMapAndAddParams(vo);
        if (!CollectionUtils.isEmpty(vo.getGmtTimeRange())) {
            if (vo.getGmtTimeRange().size() > 1) {
                paramsMap.put("startGmtTime", vo.getGmtTimeRange().get(0) + " 00:00:00");
                paramsMap.put("endGmtTime", vo.getGmtTimeRange().get(1) + " 23:59:59");
            }
        }
       List<ExtendChannelVo> vos= this.selectListBySql("selectExtendChannelList",paramsMap );
        List<Map> maps = acceptOrderInfoMapper.selectChannelReport(paramsMap);
        Map<Object,Map> totalMap = new HashMap<>();
        maps.forEach(item -> {
            totalMap.put(item.get("invite_code"),item);
        });
        List<ChannelReportVO> channelReportVOS = vos.stream().map(
                item -> {
                    ChannelReportVO channelReportVO = new ChannelReportVO();
                    channelReportVO.setChannelName(item.getChannelName());
                    channelReportVO.setLinkmanMobile(item.getLinkmanMobile());
                    channelReportVO.setLinkmanName(item.getLinkmanName());
                    channelReportVO.setBidCount(0l);
                    channelReportVO.setGuaranteeCount(0l);
                    Map map = totalMap.get(item.getInviteCode());
                    if(map!=null){
                        channelReportVO.setBidCount((Long) map.get("bidCount"));
                        channelReportVO.setGuaranteeCount(((BigDecimal) map.get("guaranteeCount")).longValue());
                    }

                    return channelReportVO;
                }
        ).collect(Collectors.toList());
        return  channelReportVOS;
    }
}
